<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 会员
 */
namespace app\ais\model;
use app\common\model\SystemUser;
use app\common\model\SystemUserLevel;
use think\Model;

class AisVipOrder extends Model{
    
    //关联用户信息
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }

    //管理会员
    public function vip(){
        return $this->hasOne('AisVip','id','vip_id');
    }

    /**
     * 创建订单
     * @param array $param
     * @return void
     */
    public static function addOrder(array $param){
        $param['state']       = 0;
        $param['create_time'] = time();
        return self::create($param);
    }

    /**
     * 开通会员收益分账
     * @param array $order  订单信息
     * @return void
     */
    public static function income($order,$money){
        //判断会员级别
        $vip = AisVip::where(['id' => $order->vip_id])->find();
        if(empty($vip)){
            return;
        }
        //扣除手续费
        $money = money($money - $money * 6 / 1000);
        //用户的直接上级会员
        $level_1 = SystemUserLevel::where(['user_id' => $order->uid,'level' => 1])->find();
        if($level_1){
            //会员推荐奖
            if($vip->open_share_reward){
                AisBank::recharge($order->member_miniapp_id,$level_1->parent_id,$vip->open_share_reward);
                AisBill::add(['member_miniapp_id' => $order->member_miniapp_id,'store_id' => 0,'money' => $vip->open_share_reward,'uid' => $level_1->parent_id],'会员推荐奖');
            }
            //会员间接奖
            if($vip->open_queue_reward){
                //查询直推是不是老板
                $is_store = AisStore::where(['manage_uid' => $level_1->parent_id])->field('id,manage_uid')->find();
                if($is_store){
                    $level_2 = SystemUserLevel::where(['user_id' => $order->uid,'level' => 2])->find();
                    if($level_2){
                        AisBank::recharge($order->member_miniapp_id,$level_2->parent_id,$vip->open_queue_reward);
                        AisBill::add(['member_miniapp_id' => $order->member_miniapp_id,'store_id' => 0,'money' => $vip->open_share_reward,'uid' => $level_2->parent_id],'商家会员推荐奖');
                    }
                }
            }
        }
        //如果采购基金大于0
       if($vip->fund > 0){
           $data = [
            'member_miniapp_id' => $order->member_miniapp_id,
            'store_id'          => 0,
            'types'             => 0,
            'state'             => 1,
            'uid'               => $order->uid,
            'money'             => $vip->fund,
            'order_no'          => $order->order_no,
            'message'           => '会员开通',
            'update_time'       => time()
           ];
           AisFundBill::create($data);
       }
    }
}